package com.huaxin.device.controller;

import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.enums.*;
import com.huaxin.device.dto.enums.schemes.SchemaMetadata;
import com.huaxin.hxenum.enums.*;
import com.huaxin.hxenum.utils.EnumUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "枚举", description = "枚举下拉选项")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/option")
public class OptionController {

    @Operation(summary = "获取设备类型枚举", description = "获取设备类型枚举")
    @GetMapping("/deviceType")
    public ResponseResult<List<EnumUtils.Option>> getDeviceTypeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(DeviceType.class));
    }

    @Operation(summary = "获取设备状态枚举", description = "获取设备状态枚举")
    @GetMapping("/deviceStatus")
    public ResponseResult<List<EnumUtils.Option>> getDeviceStatusOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(DeviceStatus.class));
    }

    @Operation(summary = "获取报警类型枚举", description = "获取报警类型枚举")
    @GetMapping("/alarmType")
    public ResponseResult<List<EnumUtils.Option>> getAlarmTypeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(AlarmType.class));
    }

    @Operation(summary = "获取告警等级枚举", description = "获取告警等级枚举")
    @GetMapping("/alarmLevel")
    public ResponseResult<List<EnumUtils.Option>> getAlarmLevelOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(AlarmLevel.class));
    }

    @Operation(summary = "获取比较运算符枚举", description = "获取比较运算符枚举")
    @GetMapping("/comparisonOperator")
    public ResponseResult<List<EnumUtils.Option>> getComparisonOperatorOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(ComparisonOperator.class));
    }

    @Operation(summary = "获取监控字段", description = "用于监控字段选项")
    @GetMapping("/metricField")
    public ResponseResult<List<SchemaMetadata.TableWithFields>> getMetricFieldOptions() {
        return ResponseResult.ok(SchemaMetadata.getAllTablesWithFields());
    }

    @Operation(summary = "获取单位枚举", description = "获取单位枚举")
    @GetMapping("/unit")
    public ResponseResult<List<EnumUtils.Option>> getUnitOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(Unit.class));
    }

    @Operation(summary = "获取断路器操作枚举", description = "获取断路器操作枚举")
    @GetMapping("/circuitBreakerAction")
    public ResponseResult<List<EnumUtils.Option>> getCircuitBreakerActionOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(CircuitBreakerAction.class));
    }

    @Operation(summary = "获取设备区域类型枚举", description = "获取设备区域类型枚举")
    @GetMapping("/deviceZone")
    public ResponseResult<List<EnumUtils.Option>> getDeviceZoneOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(DeviceZoneType.class));
    }

    @Operation(summary = "获取防水等级枚举", description = "获取防水等级枚举")
    @GetMapping("/waterproofGrade")
    public ResponseResult<List<EnumUtils.Option>> getWaterproofGradeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(WaterproofGrade.class));
    }

    @Operation(summary = "获取防护等级枚举", description = "获取防护等级枚举")
    @GetMapping("/protectionLevel")
    public ResponseResult<List<EnumUtils.Option>> getProtectionLevelOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(ProtectionLevel.class));
    }

    @Operation(summary = "获取母线材质枚举", description = "获取母线材质枚举")
    @GetMapping("/busbarMaterial")
    public ResponseResult<List<EnumUtils.Option>> getBusbarMaterialOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(BusbarMaterial.class));
    }

    @Operation(summary = "获取配电箱类型枚举", description = "获取配电箱类型枚举")
    @GetMapping("/cabinetType")
    public ResponseResult<List<EnumUtils.Option>> getCabinetTypeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(SwitchCabinetType.class));
    }

    @Operation(summary = "获取连接方式枚举", description = "获取连接方式枚举")
    @GetMapping("/connectionType")
    public ResponseResult<List<EnumUtils.Option>> getConnectionTypeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(ConnectionType.class));
    }

    @Operation(summary = "获取冷却方式枚举", description = "获取冷却方式枚举")
    @GetMapping("/coolingMethod")
    public ResponseResult<List<EnumUtils.Option>> getCoolingMethodOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(CoolingMethod.class));
    }

    @Operation(summary = "获取设备数据类型枚举", description = "获取设备数据类型枚举")
    @GetMapping("/equipmentDataType")
    public ResponseResult<List<Map<String, Object>>> getEquipmentDataTypeOptions() {
        List<Map<String, Object>> options = Arrays.stream(EquipmentDataType.values())
                .map(type -> {
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("value", type.getCode());
                    map.put("displayName", type.getDisplayName());
                    map.put("unit", type.getUnit() != null ? type.getUnit().getCode() : null);
                    map.put("description", type.getDescription());
                    return map;
                })
                .toList();
        return ResponseResult.ok(options);
    }

    @Operation(summary = "获取设备类型枚举", description = "获取设备类型枚举")
    @GetMapping("/equipmentType")
    public ResponseResult<List<EnumUtils.Option>> getEquipmentTypeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(EquipmentType.class));
    }

    @Operation(summary = "获取消防等级枚举", description = "获取消防等级枚举")
    @GetMapping("/fireRating")
    public ResponseResult<List<EnumUtils.Option>> getFireRatingOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(FireRating.class));
    }

    @Operation(summary = "获取绝缘等级枚举", description = "获取绝缘等级枚举")
    @GetMapping("/insulationLevel")
    public ResponseResult<List<EnumUtils.Option>> getInsulationLevelOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(InsulationLevel.class));
    }

    @Operation(summary = "获取相数枚举", description = "获取相数枚举")
    @GetMapping("/phaseCount")
    public ResponseResult<List<EnumUtils.Option>> getPhaseCountOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(PhaseCount.class));
    }

    @Operation(summary = "获取通风类型枚举", description = "获取通风类型枚举")
    @GetMapping("/ventilationType")
    public ResponseResult<List<EnumUtils.Option>> getVentilationTypeOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(VentilationType.class));
    }

    @Operation(summary = "获取电压等级枚举", description = "获取电压等级枚举")
    @GetMapping("/voltageLevel")
    public ResponseResult<List<EnumUtils.Option>> getVoltageLevelOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(VoltageLevel.class));
    }

    @Operation(summary = "获取电力设备运行状态枚举", description = "获取电力设备运行状态枚举")
    @GetMapping("/equipmentStatus")
    public ResponseResult<List<EnumUtils.Option>> getEquipmentStatusOptions() {
        return ResponseResult.ok(EnumUtils.toOptions(EquipmentStatus.class));
    }
}
